home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue67 / System / DIPSLIB / DIPSLIB.lst < prev    next >
Encoding:
File List  |  2001-02-06  |  4.6 KB  |  176 lines

  1.  
  2. #line 12 "DIPSLIB.C"
  3.  
  4. HINSTANCE g_hinstDll = ((void *)0);
  5.  
  6.  
  7.  
  8.  
  9.  
  10. #pragma data_seg("Shared")
  11. HHOOK hHook = ((void *)0);
  12. DWORD dwApplicationThread = 0;              
  13.  
  14. static const TCHAR g_szRegSubKey[] = "Software\\Richter\\Desktop Item Position Saver";
  15.  
  16. #pragma data_seg()
  17. #pragma comment(linker, "/section:Shared,rws")
  18.  
  19.  
  20.  
  21. void SaveListViewItemPositions(HWND hwndLV) 
  22. {
  23.    int nItem, nMaxItems = (int)SendMessageA((hwndLV), (0x1000 + 4), 0, 0L);
  24.    HKEY hkey;
  25.    DWORD dwDisposition;
  26.  
  27.    
  28.    
  29.    RegDeleteKeyA ((( HKEY ) (ULONG_PTR)((LONG)0x80000001) ), g_szRegSubKey);
  30.  
  31.    
  32.    RegCreateKeyExA ((( HKEY ) (ULONG_PTR)((LONG)0x80000001) ), g_szRegSubKey, 0, ((void *)0), (0x00000000L), (0x0002), ((void *)0), &hkey, &dwDisposition);
  33.  
  34.    for (nItem = 0; nItem < nMaxItems; nItem++) 
  35.    {
  36.       TCHAR szName[260];
  37.       POINT pt;
  38.  
  39.       
  40.       { LVITEMA _ms_lvi; _ms_lvi.iSubItem = 0; _ms_lvi.cchTextMax = (sizeof(szName) / sizeof(szName[0])); _ms_lvi.pszText = szName; SendMessageA((hwndLV), (0x1000 + 45), (WPARAM)(nItem), (LPARAM)(LVITEMA  *)&_ms_lvi);};
  41.       (BOOL)SendMessageA((hwndLV), (0x1000 + 16), (WPARAM)(int)(nItem), (LPARAM)(POINT *)(&pt));
  42.  
  43.       
  44.       RegSetValueExA (hkey, szName, 0, ( 3 ), (PBYTE) &pt, sizeof(pt));
  45.    }
  46.  
  47.    RegCloseKey(hkey);
  48. }
  49.  
  50.  
  51.  
  52. void RestoreListViewItemPositions (HWND hwndLV) 
  53. {
  54.    HKEY hkey;
  55.    LONG l = RegOpenKeyExA((( HKEY ) (ULONG_PTR)((LONG)0x80000001) ), g_szRegSubKey,
  56.       0, (0x0001), &hkey);
  57.    if (l == 0L) {
  58.       int nIndex;
  59.  
  60.       
  61.       
  62.       DWORD dwStyle = ((DWORD)GetWindowLongA(hwndLV, (-16)));
  63.       if (dwStyle & 0x0100) SetWindowLongA (hwndLV, (-16), dwStyle & ~0x0100);
  64.  
  65.       l = 0L;
  66.       for (nIndex = 0; l != 259L; nIndex++) {
  67.          TCHAR szName[260];
  68.          POINT pt;
  69.          LVFINDINFOA lvfi;
  70.          int cbValueName = (sizeof(szName) / sizeof(szName[0]));
  71.          int cbData = sizeof(pt), nItem;
  72.          DWORD dwType;
  73.  
  74.          
  75.          l = RegEnumValueA(hkey, nIndex, szName, &cbValueName, 
  76.             ((void *)0), &dwType, (PBYTE) &pt, &cbData);
  77.  
  78.          if (l == 259L) 
  79.             continue;
  80.  
  81.          if ((dwType == ( 3 )) && (cbData == sizeof(pt))) {
  82.             
  83.             
  84.             lvfi.flags = 0x0002; 
  85.             lvfi.psz = szName; 
  86.             nItem = (int)SendMessageA((hwndLV), (0x1000 + 13), (WPARAM)(int)(-1), (LPARAM)(const LVFINDINFOA *)(&lvfi));
  87.             if (nItem != -1) (BOOL)SendMessageA((hwndLV), (0x1000 + 15), (WPARAM)(int)(nItem), ((LPARAM)(DWORD)((LONG)(((WORD)((DWORD_PTR)((pt.x)) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)((pt.y)) & 0xffff))) << 16))));    
  88.          }
  89.       }
  90.       
  91.       SetWindowLongA(hwndLV, (-16), dwStyle);
  92.       RegCloseKey(hkey);
  93.    }
  94. }
  95.  
  96.  
  97.  
  98. BOOL __stdcall DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID fImpLoad) 
  99. {
  100.     if (fdwReason == 1) g_hinstDll = hinstDll;
  101.     return (1);
  102. }
  103.  
  104.  
  105.  
  106. void DIPS_OnClose (HWND hwnd)
  107. {
  108.     DestroyWindow (hwnd);
  109. }
  110.  
  111. BOOL __stdcall DIPS_DlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
  112. {
  113.     switch (uMsg) 
  114.     {
  115.         case 0x8000:
  116.             if (lParam) SaveListViewItemPositions((HWND) wParam);
  117.             else RestoreListViewItemPositions((HWND) wParam);
  118.             break;
  119.  
  120.         case (0x0010): return ((( (uMsg) == 0x0132 || (uMsg) == 0x0133 || (uMsg) == 0x0134 || (uMsg) == 0x0135 || (uMsg) == 0x0136 || (uMsg) == 0x0137 || (uMsg) == 0x0138 || (uMsg) == 0x0039 || (uMsg) == 0x002E || (uMsg) == 0x002F || (uMsg) == 0x0037 || (uMsg) == 0x0110 ) ? (BOOL)((((DIPS_OnClose))((hwnd)), 0L)) : (SetWindowLongA((hwnd), 0, (LPARAM)(LRESULT)((((DIPS_OnClose))((hwnd)), 0L))), 1)));
  121.     }
  122.    
  123.     return (0);
  124. }
  125.  
  126.  
  127.  
  128. LRESULT __stdcall GetMsgProc (int nCode, WPARAM wParam, LPARAM lParam) 
  129. {
  130.    static BOOL fFirstTime = 1;
  131.  
  132.    if (fFirstTime) 
  133.    {
  134.       fFirstTime = 0;
  135.       
  136.       CreateDialogParamA(g_hinstDll, (LPSTR)((ULONG_PTR)((WORD)(101))), ((void *)0), DIPS_DlgProc, 0L);
  137.       
  138.       PostThreadMessageA (dwApplicationThread, 0x0000, 0, 0);
  139.    }
  140.  
  141.    return (CallNextHookEx (hHook, nCode, wParam, lParam));
  142. }
  143.  
  144.  
  145.  
  146. __declspec (dllexport) BOOL __stdcall SetDIPSHook(DWORD dwThreadId) 
  147. {
  148.    BOOL fOk = 0;
  149.    
  150.    if (dwThreadId != 0) 
  151.    {
  152.       
  153.       
  154.       
  155.       dwApplicationThread = GetCurrentThreadId();
  156.  
  157.       
  158.       hHook = SetWindowsHookExA(3, GetMsgProc, g_hinstDll, dwThreadId);
  159.       fOk = (hHook != ((void *)0));
  160.       if (fOk) {
  161.          
  162.          
  163.          
  164.          fOk = PostThreadMessageA(dwThreadId, 0x0000, 0, 0);
  165.       }
  166.    } 
  167.    else 
  168.    {
  169.       fOk = UnhookWindowsHookEx(hHook);
  170.       hHook = ((void *)0);
  171.    }
  172.  
  173.    return(fOk);
  174. }
  175.  
  176.